<?php
declare (strict_types = 1);

namespace app\command\Shopify;

use app\model\Shops;
use app\service\shopify\action\rest\OrderRest;
use app\trait\PaymentTrait;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\helper\Arr;

class ShopifyOrdersPull extends Command
{
    use PaymentTrait;
    protected ?Shops $shopify;
    protected ?array $options;
    protected int $limit = 100;
    protected function configure()
    {
        // 指令配置
        $this->setName('shopify:pull')
            ->addOption('shop_id',null,Option::VALUE_OPTIONAL,'店铺ID')
            ->addOption('name',null,Option::VALUE_OPTIONAL,'店铺域名')
            ->addOption('date',null,Option::VALUE_OPTIONAL,'日期')
            ->addOption('status',null,Option::VALUE_OPTIONAL,'状态')
            ->setDescription('拉取shopify订单');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->options = $input->getOptions();
        # 获取店铺列表
        $shopList = $this->getShopList();
        if($shopList->isEmpty()) return false;
        foreach ($shopList as $shop){
            dump('******************正在拉取店铺: ' . $shop->host.' 订单***********************');
            $this->shopify = $shop;
            $rest = new OrderRest($shop->id);
            $orderList = $this->pullOrders($rest,$this->options);
            dd(count($orderList));
        }
    }

    //获取店铺列表
    protected function getShopList()
    {
        return Shops::query()
                //店铺ID
               ->when(isset($this->options['shop_id']) && !empty($this->options['shop_id']),function ($q){
                   return $q->where('id',$this->options['shop_id']);
               })
                ->when(isset($this->options['name']) && !empty($this->options['name']),function ($q){
                    return $q->where('name',$this->options['name']);
                })
             ->select();

    }

    //递归拉取订单
    protected function pullOrders($rest,$options=[],&$result=[])
    {
        $params = [
            'api_version'=>$this->shopify->version,
            'limit'=>$this->limit
        ];
        if(isset($options['status']) && !empty($options['status'])){
            $params['financial_status'] = $options['status'];
        }
        if(isset($options['date']) && !empty($options['date'])){
            $dateRange = explode('~',$options['date']);
            $params['created_at_min'] = Carbon::parse(Arr::first($dateRange))->startOfDay()->toIso8601String();
            $params['created_at_max'] = Carbon::parse(Arr::last($dateRange))->endOfDay()->toIso8601String();
        }
        $orderList = $rest->retrieve_order_list($params);
        //$orderList = $rest->retrieve_order_count($params);
        if(empty($orderList)) return $result;
        foreach ($orderList as $order){
            array_push($result,$order);
        }
        $lastOrder = Arr::last($orderList);
        $paginate = $lastOrder->getNextPageInfo();
        if($paginate){
            sleep(2);
            $params = $paginate;
            dump($params);
            return $this->pullOrders($rest,$params,$result);
        }else{
            return $result;
        }


    }

}
